package com.damei.qmui_test_demo.activity.login;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ShareActionProvider;
import android.widget.Toast;

import com.damei.qmui_test_demo.R;
import com.damei.qmui_test_demo.activity.HomeActivity;
import com.damei.qmui_test_demo.base.BaseActivity;
import com.damei.qmui_test_demo.base.TabBarActivity;
import com.damei.qmui_test_demo.httpService.HttpManager;
import com.damei.qmui_test_demo.httpService.HttpStringResponseHandler;
import com.damei.qmui_test_demo.model.LoginResponse;
import com.damei.qmui_test_demo.utils.LogUtils;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import es.dmoral.toasty.Toasty;

public class PhoneLoginActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_phone);
        ButterKnife.bind(this);

        setLightStatus(false);
    }

    /**
     * 代理商手机号登录
     */
    private void agentLoginByPhoneRequest() {
        Map<String, String> param = new HashMap<String, String>();
        param.put("agentPhone", "19916218092");
        param.put("smsCode", "5607");

        HttpManager.getInstance(this)
                .agentLoginByMobile(param, new HttpStringResponseHandler<LoginResponse>
                        (this, LoginResponse.class, true, false) {
                    @Override
                    public void onSuccess(LoginResponse loginResponse) {
                        super.onSuccess(loginResponse);
                        LogUtils.i(loginResponse);

                        saveLocalString("LOGIN_INFO", "userId", "denglong");
                        startActivity(new Intent(PhoneLoginActivity.this, TabBarActivity.class));
                        finish();
                    }
                });
    }

    /**
     * 获取验证码
     */
    private void getPhoneSmsNumberRequest() {
        String phone = "19916218092";
        HttpManager.getInstance(this).sendSmsNumberRequest(phone, new HttpStringResponseHandler<String>(this, String.class, true, true) {
            @Override
            public void onSuccess(String s) {
                super.onSuccess(s);
                Toasty.normal(PhoneLoginActivity.this, "获取验证码成功").show();
            }
        });
    }

    /**
     * 页面点击事件
     */
    @OnClick({R.id.close_btn, R.id.get_check_num_btn, R.id.login_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.close_btn:
                finish();
                break;
            case R.id.get_check_num_btn:
//                getPhoneSmsNumberRequest();
                Toasty.success(PhoneLoginActivity.this, "获取验证码成功").show();
                break;
            case R.id.login_btn:
                agentLoginByPhoneRequest();
                startActivity(new Intent(this, TabBarActivity.class));
                finish();
                break;
        }
    }
}
